package com.ocom.nettyxf.config.loadbalancer;

import com.alibaba.cloud.nacos.ribbon.NacosServer;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.AbstractLoadBalancerRule;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.Server;
import com.ocom.common.utils.CommonUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;


@RefreshScope
public class MyRandomRule extends AbstractLoadBalancerRule {

    @Value("${payhandle.version}")
    String version;

    @Override
    public Server choose(Object key) {

        ILoadBalancer loadBalancer = getLoadBalancer();
        if (loadBalancer == null) {
            return null;
        }
        List<Server> allServers = loadBalancer.getAllServers();
        if(allServers == null || allServers.size() == 0){
            return null;
        }

        //当前服务list
        List<NacosServer>  grayServers = new ArrayList<>();
        //最大版本
        List<NacosServer>  maxVerServers = new ArrayList<>();
        //其他服务list
        List<NacosServer>  nacosServers = new ArrayList<>();

        if(allServers.size() == 1) {
            Server server = allServers.get(0);
            return server;
        }

        if(!CommonUtil.isNull(version)){
            //最小版本
            String versionMax = null;
            for (Server server1 : allServers){
                //灰度策略
                NacosServer server2 = (NacosServer) server1;
                Map<String, String>  metadata = server2.getMetadata();
                String metadataVersion = metadata.get("version");
                if(version.equals(metadataVersion)){
                    grayServers.add(server2);
                }
                if(versionMax == null ){
                    versionMax = metadata.get("version");
                }
                if(metadataVersion.compareTo(versionMax)>0){
                    versionMax = metadataVersion;
                }
            }
            if(grayServers.size()>0){
                int size = grayServers.size();
                Random random = new Random();
                return grayServers.get(random.nextInt(size));
            }else{
                //返回最大版本
                for (Server server1 : allServers){
                    //灰度策略
                    NacosServer server2 = (NacosServer) server1;
                    Map<String, String>  metadata = server2.getMetadata();
                    String metadataVersion = metadata.get("version");
                    if(versionMax.equals(metadataVersion)){
                        maxVerServers.add(server2);
                    }else{
                        nacosServers.add(server2);
                    }
                }
                if(maxVerServers.size()>0){
                    int size = maxVerServers.size();
                    Random random = new Random();
                    return maxVerServers.get(random.nextInt(size));
                }else{
                    int size = nacosServers.size();
                    Random random = new Random();
                    return nacosServers.get(random.nextInt(size));
                }
            }
        }

        int size = allServers.size();
        Random random = new Random();
        return allServers.get(random.nextInt(size));
    }
    @Override
    public void initWithNiwsConfig(IClientConfig clientConfig) {

    }
}
